const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';

const baseURL = 'http://localhost:9000';

function request(method, url, data) {
  return new Promise(function(resolve, reject) {
    let header = {
      'content-type': 'application/json',
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
    };
    wx.request({
      url: baseURL + url,
      method: method,
      data:  data,
      header: header,
      success(res) {
          //请求成功
          //判断状态码---errCode状态根据后端定义来判断
          if (res.data.errCode == 200) {
            resolve(res);
          } else if (res.data.errCode == 401){
            reject('用户未登录');
          } else {
            //其他异常
            reject('运行时错误,请稍后再试');
          }
      },
      fail(err) {
          //请求失败
          reject(err)
        }
    })
  })
}

// 请求方法
const API = {
  // 登陆接口
  login: (data) => {
    return request(POST, `/user/login`, data);
  },
  // 注册接口
  register: (data) => {
    return request(POST, '/user/register', data)
  }
};

module.exports = {
  API: API
}